﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrossEntropyProject.Cross_Entropy;

namespace CrossEntropyProject
{
    internal static class Profiles
    {
        #region variables

        internal static List<CustomersCluster> customersClusters;
        internal static List<ProfileData> profilesList; 

        #endregion

        #region BuildRegularProfiles()
        internal static void BuildRegularProfiles()
        {
            customersClusters = CrossEntropy.clusters;
            profilesList = new List<ProfileData>();

            for (int i = 0; i < customersClusters.Count; i++)  //memory allocation
            {
                profilesList.Add(new ProfileData());
            }

            for (int i = 0; i < customersClusters.Count; i++)  //for all clusters
            {
                for (int j = 0; j < customersClusters[i].customers.Count; j++)  //for all customers in cluster i
                {
                    for (int k = 0; k < customersClusters[i].customers[j].Length; k++)  //for each component of customer
                    {
                        profilesList[i].meansOfProfile[k] += customersClusters[i].customers[j][k];
                    }

                }

                //now divide the sums by the number of customers in cluster i
                for (int t = 0; t < profilesList[i].meansOfProfile.Length; t++)
                {
                    profilesList[i].meansOfProfile[t] /= customersClusters[i].customers.Count;
                }
            }//for i
            //---------------------- calculate std values --------------------------------------------------------------------


            double[] theMeansArr;
            double[] customerVector;
            
            for (int i = 0; i < customersClusters.Count; i++) //for all clusters
            {
                for (int j = 0; j < customersClusters[i].customers.Count; j++) //for all customers in cluster i
                {
                    customerVector = customersClusters[i].customers[j];
                    theMeansArr = profilesList[i].meansOfProfile;
                   
                    for (int k = 0; k < customersClusters[i].customers[j].Length; k++) //for each component of customer
                    {
                        profilesList[i].stdValues[k] += Math.Pow(theMeansArr[k] - customerVector[k], 2);
                    }
                }

                for (int j = 0; j < profilesList[i].stdValues.Length; j++)
                {
                    profilesList[i].stdValues[j] = Math.Sqrt(profilesList[i].stdValues[j] / Convert.ToDouble(customersClusters[i].customers.Count));
                }
            }
            //------------------------------------------------------------------------------------------------
    
       // profilesList.Sort();

        } 
        #endregion


    }//class

    #region class ProfileData
    internal class ProfileData : IComparable<ProfileData>
    {
       internal double[] meansOfProfile;
       internal double[] stdValues;
       
        public ProfileData()
        {
            meansOfProfile = new double[PAM.k];
            stdValues = new double[PAM.k];
        }

        public int CompareTo(ProfileData value)
        {
            return this.meansOfProfile.Max().CompareTo(value.meansOfProfile.Max());
        }


    } 
    #endregion

}
